﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PresentationTier
{
    /// <summary>
    /// Partial ProjectsForm class.
    /// </summary>
    public partial class ProjectsForm : Form
    {
        /*constants*/
        private const string PROJECT_NAME = "ProjectName";
        /*objects*/
        private BusinessLogicTier.ProjectsBLT ProjectsBLT;       
        private BindingSource ProjectsBindingSource;
        private BindingSource TestBindingSource;


        private DataEntityTier.OpenRecordsDataSet.ArtistsDataTable ArtistsDataTable;
        private DataEntityTier.OpenRecordsDataSet.ProjectsDataTable ProjectsDataTable;
        private BusinessLogicTier.ArtistsBLT ArtistsBLT;
        private BindingSource ArtistsParentBindingSource;
        private BindingSource ProjectsChildBindingSource;
        private DataRelation Relation;
        private DataSet DataSet;


        /// <summary>
        /// Default constructor.
        /// </summary>
        public ProjectsForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Set bindingsource and assign bindingsource to controls.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProjectsForm_Load(object sender, EventArgs e)
        {
            this.ProjectsBLT = new BusinessLogicTier.ProjectsBLT();
            this.ProjectsBindingSource = new BindingSource();
            this.ProjectsBindingSource.DataSource = this.ProjectsBLT.GetProjects();
            this.projectsDataGridView.DataSource = this.ProjectsBindingSource;
            this.viewProjectsBindingNavigator.BindingSource = this.ProjectsBindingSource;
            this.addProjectsBindingNavigator.BindingSource = this.ProjectsBindingSource;
            this.delProjectsBindingNavigator.BindingSource = this.ProjectsBindingSource;
            this.deleteProjectsListBox.DataSource = this.ProjectsBindingSource;
            this.deleteProjectsListBox.DisplayMember = PROJECT_NAME;
            this.ArtistsBLT = new BusinessLogicTier.ArtistsBLT();
            this.ProjectsChildBindingSource = new BindingSource();
            this.ArtistsParentBindingSource = new BindingSource();

            /*create DataSet to hold parent/child relational DataTables*/
            this.DataSet = new DataSet();

            /*fill parent & child DataTables with data*/
            this.ProjectsDataTable = this.ProjectsBLT.GetProjects();
            this.ArtistsDataTable = this.ArtistsBLT.GetArtists();  
            
            /*add the parent/child Datatables to the DataSet*/
            this.DataSet.Tables.Add(this.ArtistsDataTable);
            this.DataSet.Tables.Add(this.ProjectsDataTable);           
 
            /*create the parent/chile realtion & add relation to the DataSet*/
            this.Relation = new DataRelation("ArtistsProjectsRelation", 
                this.DataSet.Tables["Artists"].Columns["ArtistID"], this.DataSet.Tables["Projects"].Columns["ArtistID"]);
            this.DataSet.Relations.Add(this.Relation);

            /*set the Artists(parents) DataTable as the datasource for the ArtistsParentBinding source*/
            this.ArtistsParentBindingSource.DataSource = this.DataSet;
            this.ArtistsParentBindingSource.DataMember = "Artists";
            
            /*set the DataMember for the projectsChildBindingSource as the relation*/
            this.ProjectsChildBindingSource.DataSource = this.ArtistsParentBindingSource;
            this.ProjectsChildBindingSource.DataMember = "ArtistsProjectsRelation";
            
            /* set the datasources for the controls*/
            this.artistNameListBox.DataSource = this.ArtistsParentBindingSource;
            this.artistNameListBox.DisplayMember = "ArtistName";
            this.projectNameListBox.DataSource = this.ProjectsChildBindingSource;
            this.projectNameListBox.DisplayMember = "ProjectName";
        }

        /// <summary>
        /// InsertProjectsButton click event calls the insert method from theProjectsBLT class.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InsertProjectButton_Click(object sender, EventArgs e)
        {
            /*insert event with paramter values from event textboxe controls*/
            this.ProjectsBLT.InsertProject("2", "3", projectNameTextBox.Text, 
                projectMyspaceTextBox.Text, projectFacebookTextBox.Text, projectEmailTextBox.Text);

            MessageBox.Show(projectNameTextBox.Text + " project inserted.", "Insert Confirmation");

            /*reset bindingsource after insert has executed*/
            this.ProjectsBindingSource.DataSource = this.ProjectsBLT.GetProjects();
            this.ProjectsBindingSource.ResetBindings(true);
        }

        /// <summary>
        /// DeleteProjectsButton click event calls the delete method from theProjectsBLT class.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteProjectButton_Click(object sender, EventArgs e)
        {
            /*delete artist that user selected from the artistNameListbox control*/
            string projectsId = this.projectsDataGridView.Rows[this.projectsDataGridView.CurrentRow.Index].Cells[0].Value.ToString();
            this.ProjectsBLT.DeleteProject(projectsId);

            MessageBox.Show("Project has been deleted.", "Delete Confirmation");

            /*reset bindingsource after insert has executed*/
            this.ProjectsBindingSource.DataSource = this.ProjectsBLT.GetProjects();
            this.ProjectsBindingSource.ResetBindings(true);
        }
    }
}
